using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace ICP_MS.Plugin.LA300;

public class FormStandard : Form
{
	private ExperimentDatas experiment = null;

	private ExperimentDB db = null;

	private DataTable dtUnit = new DataTable();

	private IContainer components = null;

	private DbufferGrid dgvInternalIsotopes;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn5;

	private ComboBox cbISTD;

	private Label label1;

	private DataGridViewTextBoxColumn colSample;

	private DataGridViewTextBoxColumn colConcentration;

	private DataGridViewComboBoxColumn colUnit;

	public FormStandard()
	{
		InitializeComponent();
	}

	public void SetParams(ExperimentDatas experiment, ExperimentDB db)
	{
		this.experiment = experiment;
		this.db = db;
	}

	private void FormStandard_Load(object sender, EventArgs e)
	{
		base.WindowState = FormWindowState.Maximized;
		dtUnit.Columns.Add("UnitType", typeof(string));
		dtUnit.Columns.Add("UnitValue", typeof(int));
		dtUnit.Rows.Add("%", 0);
		dtUnit.Rows.Add("ppm", 1);
		dtUnit.Rows.Add("ppb", 2);
		dtUnit.Rows.Add("ppt", 3);
		dtUnit.Rows.Add("ppq", 4);
		colUnit.DataSource = dtUnit;
		colUnit.DisplayMember = "UnitType";
		colUnit.ValueMember = "UnitValue";
		SetInternalStandards();
		InitInternalStandards();
		experiment.isotopeChanged += experiment_isotopeChanged;
		experiment.sampleChanged += experiment_sampleChanged;
		ApplyResources();
	}

	private void ApplyResources()
	{
		Text = Lang.Txt("FormStandard.Title.Text");
		colConcentration.HeaderText = Lang.Txt("FormStandard.Columns.Concentration.Text");
		colUnit.HeaderText = Lang.Txt("FormStandard.Columns.Unit.Text");
		colSample.HeaderText = Lang.Txt("FormStandard.Columns.Sample.Text");
		label1.Text = Lang.Txt("FormStandard.InterStd.Text");
	}

	private void FormStandard_FormClosing(object sender, FormClosingEventArgs e)
	{
		experiment.isotopeChanged -= experiment_isotopeChanged;
		experiment.sampleChanged -= experiment_sampleChanged;
	}

	private void InitInternalStandards()
	{
		((DataGridView)(object)dgvInternalIsotopes).AutoGenerateColumns = false;
		if (((DataGridView)(object)dgvInternalIsotopes).DataSource == null)
		{
			((DataGridView)(object)dgvInternalIsotopes).DataSource = experiment.SampleIstds;
			return;
		}
		BindingContext[((DataGridView)(object)dgvInternalIsotopes).DataSource].SuspendBinding();
		((DataGridView)(object)dgvInternalIsotopes).DataSource = null;
		((DataGridView)(object)dgvInternalIsotopes).DataSource = experiment.SampleIstds;
		BindingContext[((DataGridView)(object)dgvInternalIsotopes).DataSource].ResumeBinding();
	}

	private void experiment_sampleChanged(object sender, EventArgs e)
	{
		InitInternalStandards();
	}

	private void experiment_isotopeChanged(object sender, EventArgs e)
	{
		SetInternalStandards();
	}

	private void SetInternalStandards()
	{
		BindInternalStandards();
	}

	private void BindInternalStandards()
	{
		Invoke((MethodInvoker)delegate
		{
			cbISTD.Items.Clear();
			IEnumerable<string> source = experiment.Isotopes.Select((IsotopeInfos isotope) => isotope.symbol);
			IEnumerable<string> source2 = from isotope in experiment.Isotopes
				where isotope.inter
				select isotope.symbol;
			cbISTD.Items.Add(Lang.Txt("FormExperiment.ConstNouns.ForceNone.Text"));
			ComboBox.ObjectCollection items = cbISTD.Items;
			object[] items2 = source.ToArray();
			items.AddRange(items2);
			if (source2.Any())
			{
				cbISTD.SelectedItem = source2.First();
			}
			else
			{
				cbISTD.SelectedIndex = 0;
			}
		});
	}

	private void dgvInternalStandards_CurrentCellChanged(object sender, EventArgs e)
	{
	}

	private double UnitReConvert(int unit, double value)
	{
		return unit switch
		{
			_ => value, 
		};
	}

	private double UnitConverte(int unit, double value)
	{
		return unit switch
		{
			_ => value, 
		};
	}

	private void StandardChagnedNotify()
	{
		experiment.OnMethodChanged();
	}

	public void SelectedChanged(object sender, EventArgs e)
	{
	}

	private void cbISTD_SelectedIndexChanged(object sender, EventArgs e)
	{
		string symbol = cbISTD.SelectedItem.ToString();
		foreach (IsotopeInfos isotope in experiment.Isotopes)
		{
			isotope.inter = isotope.symbol == symbol;
		}
		StandardChagnedNotify();
	}

	private void dgvInternalIsotopes_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
	{
		if (e.RowIndex < 0)
		{
			return;
		}
		int columnIndex = e.ColumnIndex;
		int num = columnIndex;
		if (num == 1)
		{
			if (experiment.SampleIstds[e.RowIndex].Concentration <= 0.0)
			{
				e.Value = "";
			}
			else
			{
				e.Value = UnitConverte(experiment.SampleIstds[e.RowIndex].Unit, experiment.SampleIstds[e.RowIndex].Concentration);
			}
		}
	}

	private void dgvInternalIsotopes_CellValueChanged(object sender, DataGridViewCellEventArgs e)
	{
		if (e.RowIndex >= 0)
		{
			switch (e.ColumnIndex)
			{
			case 1:
			{
				SampleISTD istd = experiment.SampleIstds[e.RowIndex];
				istd.Concentration = UnitReConvert(istd.Unit, istd.Concentration);
				break;
			}
			case 2:
				((Control)(object)dgvInternalIsotopes).Refresh();
				break;
			}
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Expected O, but got Unknown
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
		this.cbISTD = new System.Windows.Forms.ComboBox();
		this.label1 = new System.Windows.Forms.Label();
		this.dgvInternalIsotopes = new DbufferGrid();
		this.colSample = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colConcentration = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colUnit = new System.Windows.Forms.DataGridViewComboBoxColumn();
		this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridViewTextBoxColumn3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridViewTextBoxColumn4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridViewTextBoxColumn5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		((System.ComponentModel.ISupportInitialize)this.dgvInternalIsotopes).BeginInit();
		base.SuspendLayout();
		this.cbISTD.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cbISTD.FormattingEnabled = true;
		this.cbISTD.Location = new System.Drawing.Point(176, 49);
		this.cbISTD.Name = "cbISTD";
		this.cbISTD.Size = new System.Drawing.Size(121, 20);
		this.cbISTD.TabIndex = 11;
		this.cbISTD.SelectedIndexChanged += new System.EventHandler(cbISTD_SelectedIndexChanged);
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(102, 52);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(59, 12);
		this.label1.TabIndex = 12;
		this.label1.Text = "内标元素:";
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).AllowUserToAddRows = false;
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).AllowUserToDeleteRows = false;
		((System.Windows.Forms.Control)(object)this.dgvInternalIsotopes).Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).BackgroundColor = System.Drawing.SystemColors.Control;
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
		dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle1.Font = new System.Drawing.Font("宋体", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).Columns.AddRange(this.colSample, this.colConcentration, this.colUnit);
		dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
		dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
		dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).DefaultCellStyle = dataGridViewCellStyle2;
		this.dgvInternalIsotopes.HorCopy = false;
		((System.Windows.Forms.Control)(object)this.dgvInternalIsotopes).Location = new System.Drawing.Point(12, 118);
		((System.Windows.Forms.Control)(object)this.dgvInternalIsotopes).Name = "dgvInternalIsotopes";
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).RowTemplate.Height = 23;
		((System.Windows.Forms.Control)(object)this.dgvInternalIsotopes).Size = new System.Drawing.Size(631, 417);
		((System.Windows.Forms.Control)(object)this.dgvInternalIsotopes).TabIndex = 9;
		this.dgvInternalIsotopes.VerCopy = false;
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(dgvInternalIsotopes_CellFormatting);
		((System.Windows.Forms.DataGridView)(object)this.dgvInternalIsotopes).CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(dgvInternalIsotopes_CellValueChanged);
		this.colSample.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.colSample.DataPropertyName = "SampleName";
		this.colSample.HeaderText = "样品";
		this.colSample.Name = "colSample";
		this.colSample.ReadOnly = true;
		this.colConcentration.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.colConcentration.DataPropertyName = "Concentration";
		this.colConcentration.HeaderText = "浓度";
		this.colConcentration.Name = "colConcentration";
		this.colUnit.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.colUnit.DataPropertyName = "Unit";
		this.colUnit.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.Nothing;
		this.colUnit.HeaderText = "单位";
		this.colUnit.Name = "colUnit";
		this.dataGridViewTextBoxColumn1.DataPropertyName = "symbol";
		this.dataGridViewTextBoxColumn1.HeaderText = "符号";
		this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
		this.dataGridViewTextBoxColumn1.ReadOnly = true;
		this.dataGridViewTextBoxColumn1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
		this.dataGridViewTextBoxColumn1.Width = 80;
		this.dataGridViewTextBoxColumn2.DataPropertyName = "technique";
		this.dataGridViewTextBoxColumn2.HeaderText = "方法";
		this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
		this.dataGridViewTextBoxColumn2.ReadOnly = true;
		this.dataGridViewTextBoxColumn2.Resizable = System.Windows.Forms.DataGridViewTriState.True;
		this.dataGridViewTextBoxColumn2.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
		this.dataGridViewTextBoxColumn2.Width = 120;
		this.dataGridViewTextBoxColumn3.DataPropertyName = "symbol";
		this.dataGridViewTextBoxColumn3.HeaderText = "符号";
		this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
		this.dataGridViewTextBoxColumn3.ReadOnly = true;
		this.dataGridViewTextBoxColumn3.Width = 80;
		this.dataGridViewTextBoxColumn4.DataPropertyName = "concentration";
		this.dataGridViewTextBoxColumn4.HeaderText = "浓度";
		this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4";
		this.dataGridViewTextBoxColumn4.Width = 60;
		this.dataGridViewTextBoxColumn5.DataPropertyName = "units";
		this.dataGridViewTextBoxColumn5.HeaderText = "单位";
		this.dataGridViewTextBoxColumn5.Name = "dataGridViewTextBoxColumn5";
		this.dataGridViewTextBoxColumn5.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
		this.dataGridViewTextBoxColumn5.Width = 60;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(778, 547);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.cbISTD);
		base.Controls.Add((System.Windows.Forms.Control)(object)this.dgvInternalIsotopes);
		base.Name = "FormStandard";
		base.ShowIcon = false;
		this.Text = "FormStandard";
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormStandard_FormClosing);
		base.Load += new System.EventHandler(FormStandard_Load);
		((System.ComponentModel.ISupportInitialize)this.dgvInternalIsotopes).EndInit();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
